//
//  ARBuyerCell.m
//  AriseSamePro
//
//  Created by 包日守 on 15/11/3.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARBuyerCell.h"
#import "UIImageView+WebCache.h"
#import "UIImageView+ARRessume.h"
@interface ARBuyerCell ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;




@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property (weak, nonatomic) IBOutlet UILabel *remainLabel;

@property (weak, nonatomic) IBOutlet UIButton *buyButton;


@end

@implementation ARBuyerCell

- (IBAction)shareButtonClick:(id)sender {
    
    
    if (self.ShareBtnBlock) {
        self.ShareBtnBlock(self.cateModel,sender);
    }
}


- (void) setCateModel:(ARCate5Model *)cateModel
{

    
    [self.marrayOfImages removeAllObjects];
    
    for (UIView *view in self.scrollView.subviews) {
        [view removeFromSuperview];
    }
    
    self.nameLabel.text = cateModel.mediaModel.productModel.title;
    self.remainLabel.text = [NSString stringWithFormat:@"还剩%@件",cateModel.mediaModel.productModel.count_remaining];
    if ([cateModel.mediaModel.productModel.count_remaining isEqualToString:@"0"]) {
        [self.buyButton setTitle:@"已售罄" forState:UIControlStateNormal];
        [self.buyButton setBackgroundColor:[UIColor lightGrayColor]];
        [self.buyButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.buyButton.userInteractionEnabled = NO;
    }
    else
    {
        
        float price = cateModel.mediaModel.productModel.price.floatValue / 100;
        
        [self.buyButton setTitle:[NSString stringWithFormat:@"￥%0.2f",price] forState:UIControlStateNormal];
        [self.buyButton setBackgroundColor:[UIColor orangeColor]];
        [self.buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    }
    
    self.contentLabel.text = cateModel.mediaModel.productModel.desc;
    

    NSArray *arrayOfImages = [cateModel.mediaModel.productModel.pictures componentsSeparatedByString:@","];
    
    int i = 0;
    for (NSString *strURL in arrayOfImages) {
        NSURL *url = [NSURL URLWithString:strURL];
        
        
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(375 * i, 0, 375, 380)];
        [self.scrollView addSubview:imageV];
        
       //[imageV sd_setImageWithURL:url];
        
        [imageV ar_setImageWithURL:strURL];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dealWithTap:)];
        [imageV addGestureRecognizer:tap];
        
        imageV.userInteractionEnabled = YES;
        imageV.tag = i;
        
        [self.marrayOfImages addObject:imageV];
        
        i ++;
    }

    self.scrollView.pagingEnabled = YES;
    
    self.scrollView.contentSize = CGSizeMake(375 * arrayOfImages.count, 380);
    self.scrollView.delegate = self;

    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.pageControl.numberOfPages = arrayOfImages.count;
    [self.pageControl addTarget:self action:@selector(pageControlChange) forControlEvents:UIControlEventValueChanged];
    
}

- (void) dealWithTap:(UITapGestureRecognizer *) tap
{
    if (self.tapImageViewsBlock) {
        self.tapImageViewsBlock((int)tap.view.tag);
    }
}

- (IBAction)buyButtonClick:(id)sender {
}




- (void)awakeFromNib {
    // Initialization code
    
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    self.marrayOfImages = [[NSMutableArray alloc] init];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void) pageControlChange
{
    self.scrollView.contentOffset = CGPointMake([UIScreen mainScreen].bounds.size.width * self.pageControl.currentPage, 0);

}
#pragma mark - scrollViewDelegate
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int index = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
    self.pageControl.currentPage = index;
    

}

@end
